<!-- 购物车 -->

<template>
	<view>
		<uni-nav-bar dark :fixed="true" shadow background-color="#fff" status-bar left-icon="left" title="购物车" @clickLeft="back" />
		<!-- 购物车列表 -->
		<view class="cart_list">
			<view v-if="show==true">
				<view class="goods-detail" v-for="(item,index) in store.goods" :key="index">
					<view class="detail-left">
						<view class="goods-left">
							<checkbox-group @change="selected(item)">
								<label>
									<checkbox class="selected" color="#555555" :checked="checked" /><text></text>
								</label>
							</checkbox-group>
							<image :src="item.cover_img" style="width: 144rpx;height: 144rpx;border-radius: 20rpx;margin-left: 26rpx;"></image>
						</view>
						<view class="size">
							<text class="goods-name">{{item.name}}</text>
							<text class="goods-size">{{item.size.name}}</text>
							<view class="goods-line">
								<text class="goods-price">￥{{item.size.price}}</text>
								<view class="detail-right">
									<text class="subtract" @click="operation(item, 2)">-</text>
									<text class="num">{{item.total}}</text>
									<text @click="operation(item, 1)" class="add">+</text>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 猜你喜欢 -->
		<view class="like" v-if="data.type == 1">
			<view class="like-title">
				猜你喜欢
			</view>
				<view class="waterfall">
				<!-- 左边瀑布 -->
				<view class="wt-left wt-list">
					<view class="wt-item" v-for="(good,index) in LeftList" :key="index" @click="shopdetail(good.id)">
						<view class="item-img">
							<image :src="good.cover_img" mode='widthFix'></image>
						</view>
						<!-- 介绍部分 -->
						<view class="introduce-section">
							<text class="title">{{good.name}}</text>
							<view class="tags-box">
								<text>¥{{good.price}}</text>
							</view>
						</view>
					</view>
				</view>
				<!-- 右边瀑布 -->
				<view class="vt-right wt-list">
					<view class="wt-item" v-for="(good,index) in RightList" :key="index" @click="shopdetail(good.id)">
						<view class="item-img">
							<image :src="good.cover_img" mode='widthFix'></image>
						</view>
						<!-- 介绍部分 -->
						<view class="introduce-section">
							<text class="title">{{good.name}}</text>
							<view class="tags-box">
								<text>¥{{good.price}}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 底部操作栏 -->
		<view class="end">
			<view class="end-left">
				<checkbox-group @change="selectgoods()">
					<label>
						<checkbox :checked="allchecked" color="#212029" />全选
					</label>
				</checkbox-group>
				<view>
					总计：<text style="color: #D20015;font-weight: bold;">￥ {{totalPrice}}</text>
				</view>
			</view>
			<view class="end-right" @click="goCart()">结算({{totalNumber}})</view>
		</view>
		<TabBar :current="2"></TabBar>
	</view>
</template>

<script setup>
	import { onLoad } from "@dcloudio/uni-app";
	import { ref, reactive, watch, onMounted } from "vue";
	import TabBar from '@/components/TabBar.vue';
	import { useCartStore } from '@/store/cart.js';
	import { goodsList } from '@/request/api/shop.js';

	const store = useCartStore();
	const totalNumber = ref(0);  // 总应付数量
	const totalPrice = ref(0);  // 购物车买的数量
	
	// 左侧数据列表
	var LeftList = ref([])
	// 右侧数据列表
	var RightList = ref([])
	
	const show = ref(true);
	const allchecked = ref(true);
	const checked = ref(true);
	
	const data = reactive({
		goods: [],
		list: [],
		pages: {
			page: 1,
			limit: 10,
			category_id: 1,
		},
		type: null // 0 门票  1 商品
	})
	
		
	onMounted(() => {
		getShopList();
		
		// 计算总价格跟数量
		for (let i = 0; i < store.goods.length; i++) {
			totalPrice.value += Number(store.goods[i].size.price) * Number(store.goods[i].total);
			totalNumber.value +=  Number(store.goods[i].total);
		}
	})

	onLoad((e) => {
		data.type = Number(e.type);
	});
	
	// 商品列表
	const getShopList = async () => {
		const resultData = await goodsList(data.pages);
		console.log(resultData)
		data.list = resultData.data.list;
		// 将商品信息列表分为左侧右侧两个部分
		for (let i = 0; i < data.list.length; i++) {
			if (i % 2 === 0) {
				LeftList.value.push(data.list[i])
			} else {
				RightList.value.push(data.list[i])
			}
		}
	}

	const back = () => {
		uni.navigateBack({
			delta: 1
		})
	}
	
	const change = (e) => {
		console.log(e)
	}
	
	// 计算前往确认订单页
	const goCart = () => {
		if (totalNumber.value > 0) {
			uni.navigateTo({
				url: '/pages/shop/shoporder'
			})
		} else {
			uni.showToast({ title: '请选择商品！', icon: 'none' });
		}
	}
	
	const selected = (item) => {
		item.flag = !item.flag
		if (!item.flag) {
			allchecked.value = false
	
		} else {
			const a = this.goods.forEach((item) => {
				return item.flag === true
			})
			if (a) {
				allchecked.value = true
			} else {
				allchecked.value = false
			}
		}
	}
	
	const selectgoods = () => {
		allchecked.value = !allchecked.value;
		if (this.allchecked) {
			this.goods.map(item => {
				this.checked = true
				item.flag = true
			})
		} else {
			this.checked = false
			this.goods.map(item => {
				item.flag = false
			})
		}
	}
	
	// 操作购物车
	const operation = ((item, index) => {
		store.editGoods(item, index);
	});
	
	watch(store.goods,  (newValue, oldValue) => {
			totalPrice.value = 0;
			totalNumber.value = store.goods.reduce((sum, item) => sum + Number(item.total), 0);
			for (let i = 0; i < store.goods.length; i++) {
				totalPrice.value += Number(store.goods[i].price) * Number(store.goods[i].total);
			}
		}, { deep: true }
	);
</script>


<style lang="scss">
	.goods-detail {
		width: 100%;
		height: 184rpx;
		display: flex;
		align-items: center;
	}
	.goods {
		line-height: 80rpx;
		background-color: #FFFFFF;
		&-detail {
			display: flex;
			background-color: #fff;
			justify-content: space-between;
			border-bottom: 5rpx solid #F1F1F1;
			align-items: center;
			.detail-left {
				width: 100%;
				height: 144rpx;
				display: flex;
				.goods-left {
					display: flex;
					align-items: center;
				}
				.size {
					// width: 500rpx;
					flex: 1;
					.goods-name {
						width: 450rpx;
						white-space:nowrap;
						overflow:hidden;
						text-overflow:ellipsis;
					}
					.goods-size {
						font-size: 26rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #9C9C9C;
						line-height: 40rpx;
					}
				}
			}

			.size {
				display: flex;
				justify-content: space-between;
				flex-direction: column;
				margin-left: 30rpx;
				.goods-name {
					flex: 1;
					font-size: 26rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #1C1919;
					line-height: 40rpx;
				}
				.goods-line {
					display: flex;
					align-items: center;
					justify-content: space-between;
					.goods-price {
						font-size: 25rpx;
						color: #212029;
					}
				}
			}

			.detail-right {
				text {
					width: 50rpx;
					line-height: 50rpx;
					text-align: center;
					display: inline-block;
					background-color: #F7F7F7;
					margin-right: 10rpx;
				}

				.add {
					color: #fff;
					margin-right: 20rpx;
					background-color: #212029;
				}

				.subtract {
					background-color: #fff;
					color: #212029;
					border: 1rpx solid #212029;
				}
			}
		}
	}

	.empty {

		position: relative;
		top: 220rpx;
		text-align: center;
		display: flex;
		align-items: center;
		flex-direction: column;

		&-text {
			color: #808080;
			margin-bottom: 50rpx;
		}

		&-button {
			width: 300rpx;
			height: 90rpx;
			color: orange;
			border: 1rpx solid orange;
			text-align: center;
			line-height: 90rpx;
			border-radius: 48rpx;
		}
	}
	
	.like {
		box-sizing: border-box;
		.like-title {
			margin-left: 36rpx;
			margin-top: 40rpx;
			font-size: 24rpx;
			font-family: PingFangSC-Medium, PingFang SC;
			font-weight: 500;
			color: #211F1F;
			line-height: 40rpx;
			display: inline-block;
			border-bottom: 2rpx solid #D20015;
			padding-bottom: 10rpx;
		}
	}
	
	// 瀑布流
	.waterfall {
		display: flex;
		flex-direction: row;
		justify-content: center;
		overflow: hidden;
		padding: 40rpx 50rpx;
		box-sizing: border-box;
		padding-bottom: 300rpx;
	}
	
	.wt-list {
		width: 50%;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		margin-right: 26rpx;
	}
	.wt-list:last-child {
		margin-right: 0rpx;
	}
	
	.wt-item {
		margin-bottom: 15rpx;
		background-color: #ffffff;
	}
	
	.item-img image {
		width: 100%;
		border-radius: 80rpx;
	}
	
	/* 商品介绍 */
	.introduce-section {
		background: #ffffff;
		padding: 20rpx 30rpx;
	}
	
	.introduce-section .title {
		display: inline-block;
		font-size: 32rpx;
		color: #303133;
		font-size: 24rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #211F1F;
		line-height: 60rpx;
		width: 300rpx;
		height: 40rpx;
		overflow: hidden;//溢出隐藏
		text-overflow: ellipsis;//超出省略号显示
		white-space: nowrap;//文字不换行
	}
	
	.introduce-section .tags-box {
		font-size: 24rpx;
		color: #909399;
		font-size: 24rpx;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 600;
		color: #211F1F;
	}

	.cart_list {
		padding: 0rpx 24rpx;
		box-sizing: border-box;
		image {
			width: 144rpx;
			height: 144rpx;
			border-radius: 20rpx;
		}
	}

	.end {
		padding: 26rpx 36rpx;
		box-sizing: border-box;
		width: 100%;
		height: 90rpx;
		background-color: #fff;
		left: 0;
		display: flex;
		align-items: center;
		position: fixed;
		bottom: 220rpx;
		font-size: 26rpx;
		font-family: PingFangSC-Semibold, PingFang SC;
		font-weight: 600;
		color: #000000;
		line-height: 40rpx;
		&-left {
			width: 70%;
			display: flex;
			justify-content: space-between;
			padding: 0 30rpx;
			
			.end-flex {
				display: flex;
				align-items: center;
			}
		}

		&-right {
			width: 218rpx;
			height: 86rpx;
			line-height: 86rpx;
			background-color: #212029;
			text-align: center;
			border-radius: 40rpx;
			color: #fff;
		}
	}
</style>